package com.hongmeng.web.controller.operater.aliyun;

import com.alibaba.fastjson.JSONObject;
import com.hongmeng.operater.listener.vodHandle.IVodCallbackHandler;
import com.hongmeng.operater.listener.vodHandle.VodBeanPostProcessor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

@RestController
@RequestMapping("/vod")
/**
 * 阿里云vod回调处理 回调设置为：
 * http://139.196.240.182:8070/medmoment/vod/callback
 * 其中ip和端需要实际修改。 目前设置地址从阿里云平台设置 未接入api
 */
@Slf4j
public class AliyunVodCallbackController {

    @RequestMapping("/callback")
    public void vodCallBack(HttpServletRequest request) throws IOException {

      InputStream inputStream = request.getInputStream();

        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder sb = new StringBuilder();
        String temp = null;
        while ( (temp =  reader.readLine()) !=  null){
            sb.append(temp);
        }
        log.info("vod回调返回json格式");
        log.info(sb.toString());
        JSONObject jsonObject =  JSONObject.parseObject(sb.toString());
        IVodCallbackHandler vodCallbackHandler = VodBeanPostProcessor.vodCallbackPool.get(jsonObject.getString("EventType"));
        if(vodCallbackHandler!=null) {
            vodCallbackHandler.handelResult(jsonObject);
        }else {
            log.warn("Vod回调时间：{},无监听",jsonObject.getString("EventType"));
        }
    }
}
